package com.orange.off.cloud.cloudoff.views;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.model.CertifiedDemand_ListBean;

import java.util.List;

/**
 * 我的客户-推荐客户自定义PopupWindow  主要用来显示ListView
 *
 * @param <T>
 * @param <T>
 * @author Ansen
 * @create time 2015-11-3
 */
public class ClientAddSpinerPopWindow<T> extends PopupWindow {
    private LayoutInflater inflater;
    private ListView mListView;
    private List<T> list;
    private MyAdapter mAdapter;
    private Context context;
    /**
     * 我的客户-推荐客户自定义PopupWindow  主要用来显示ListView
     *
     */
    public ClientAddSpinerPopWindow(Context context, List<T> list, OnItemClickListener clickListener) {
        super(context);
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        init(clickListener);
    }

    private void init(OnItemClickListener clickListener) {
        View view = inflater.inflate(R.layout.spiner_window_layout, null);
        setContentView(view);
        setWidth(LayoutParams.WRAP_CONTENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);
        mListView = (ListView) view.findViewById(R.id.listview);
        mAdapter = new MyAdapter(context);
        mListView.setAdapter(mAdapter);
        setData(list);
        mListView.setOnItemClickListener(clickListener);
    }

    public void setData(List<T> data) {
        mAdapter.setData(data);
    }

    private class MyAdapter extends ArrayAdapter<T> {


        public MyAdapter(Context context) {
            super(context, android.R.layout.simple_list_item_2);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void setData(List<T> data) {
            clear();
            if (data != null) {
                for (T Bean : data) {
                    add(Bean);
                }
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.spiner_item_layout, null);
                holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            CertifiedDemand_ListBean bean = (CertifiedDemand_ListBean) getItem(position);
            holder.tvName.setText(bean.con);
            return convertView;
        }
    }

    private class ViewHolder {
        private TextView tvName;
    }
}
